package com.central.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.central.common.utils.ConvertUtils;
import com.central.modules.sys.dao.SysNoticeDao;
import com.central.modules.sys.entity.SysNoticeEntity;
import com.central.modules.sys.model.AddSysNoticeRequest;
import com.central.modules.sys.model.SysNoticeQueryBean;
import com.central.modules.sys.service.SysNoticeService;
import org.springframework.stereotype.Service;

@Service
public class SysNoticeServiceImpl extends ServiceImpl<SysNoticeDao, SysNoticeEntity> implements SysNoticeService {
    @Override
    public void add(AddSysNoticeRequest request) {
        SysNoticeEntity noticeEntity = ConvertUtils.sourceToTarget(request, SysNoticeEntity.class);
        this.save(noticeEntity);
    }

    @Override
    public IPage<SysNoticeEntity> searchPage(SysNoticeQueryBean queryBean) {
        Page<SysNoticeEntity> page = new Page<>(queryBean.getPageStart(), queryBean.getPageSize());
        LambdaQueryWrapper<SysNoticeEntity> queryWrapper = new LambdaQueryWrapper<SysNoticeEntity>();
        queryWrapper.orderByDesc(SysNoticeEntity::getCreator);
        return this.page(page, queryWrapper);
    }

    @Override
    public void updateById(Long id, AddSysNoticeRequest request) {
        SysNoticeEntity noticeEntity = ConvertUtils.sourceToTarget(request, SysNoticeEntity.class);
        this.updateById(noticeEntity);
    }

    @Override
    public void deleteById(Long id) {
        this.removeById(id);
    }
}
